package com.pvdnc.nsocket.protocol;

import com.pvdnc.nsocket.protocol.base.Packet;

import java.util.concurrent.atomic.AtomicReference;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class Encoder extends MessageToByteEncoder<Packet> {

    private static final AtomicReference<Encoder> sInstance=new AtomicReference<>();

    public static Encoder getInstance(){
        if(sInstance.get()==null)
            sInstance.set(new Encoder());
        return sInstance.get();
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Packet msg, ByteBuf out) throws Exception {
        PacketCodec.getInstance().encode(msg,out);
    }
}
